# Code for Table O2: Complier Comparison
rm(list = ls())

compliers <- function(group = "All") {
  
  ## ---------------------------------------
  ## Load Packages 
  ## ---------------------------------------
  
  library('AER')
  library('ivpack')
  library('data.table')

  ## ---------------------------------------
  ## Load Data and Functions
  ## ---------------------------------------
  load("../data0812.RData")
  
  directory <- "../functions/"
  functions <- list.files(directory)  
  loadfunctions <- sapply(functions, FUN = function(x)source(paste0(directory, x)))
  
  ## ---------------------------------------
  # Construct Instrument
  ## ---------------------------------------
  data0812 <- constructIV(data0812)
  
  ## ---------------------------------------
  #  Select Last Case Before Election
  ## ---------------------------------------
  data0812 <- lastCase(data0812)
  
  ## ---------------------------------------
  ## Construct additional covariate measures
  ## ---------------------------------------
  data0812$quant_1 <-ifelse(data0812$quants_inc == 1,1,0)
  data0812$quant_2 <-ifelse(data0812$quants_inc == 2,1,0)
  data0812$quant_3 <-ifelse(data0812$quants_inc == 3,1,0)
  data0812$quant_1[is.na(data0812$quant_1)] <- 0
  data0812$quant_2[is.na(data0812$quant_2)] <- 0
  data0812$quant_3[is.na(data0812$quant_3)] <- 0
  data0812$quant_unk <-ifelse(is.na(data0812$quants_inc) ,1,0)
  
  data0812$Black <- ifelse(data0812$race == "Black", 1, 0)
  data0812$White <- ifelse(data0812$race == "White", 1, 0)
  data0812$Male <- ifelse(data0812$Male == 1, 1, 0)

  if(group == "Black") {
    data0812 <- data0812[Black == 1, ]
  } 
  if(group == "White") {
    data0812 <- data0812[White == 1, ]
  }
  if(group == "Bottom Quantile") {
    data0812 <- data0812[quant_1 == 1, ]
  }
  if(group == "Mid Quantile") {
    data0812 <- data0812[quant_2 == 1, ]
  }
  if(group == "Upper Quantile") {
    data0812 <- data0812[quant_3 == 1, ]
  }
  
  ## ---------------------------------------
  ## Two Stage Least Squares Specifications
  ## ---------------------------------------
  
  time.controls <- "as.factor(court_time1) + as.factor(court_time2) + as.factor(court_dow) + as.factor(court_shift)"
  case.controls <- "as.factor(any_drug_2) + as.factor(fire_arms_2) +  as.factor(any_rob_2) + as.factor(any_dui_2) + as.factor(prior_offender_2)"
  demo.controls <- "age_2012 + I(age_2012^2) + Female + as.factor(race) + vote2008 + as.factor(noteli08) + regis_before"
  
  outc.1 <- "vote2012"
  endo.1 <- "pti"
  inst.1 <- "judgeiv"
  
  
  data0812$comp_w <- NA
  
  for(i in 1:3) {
      form.3.cc <- formula(paste(endo.1, "~", inst.1, "+", time.controls))
      out <- lm(form.3.cc, data = data0812[totOGS2 == i, ])  
      quants.iv <- quantile(data0812$judgeiv[data0812$totOGS2 == i], c(0.01, 0.99))
      frac_complier <- out$coefficients["judgeiv"] * quants.iv[2] - out$coefficients["judgeiv"] * quants.iv[1]
      mean_frac <- sum(data0812$totOGS2 == i)/nrow(data0812)
      data0812$comp_w[data0812$totOGS2 == i] <- frac_complier/mean_frac
  }
  
  
  w.mean <- function(x, y) { sum(x * y,na.rm=T)/sum(y,na.rm=T) }
  se.w.mean <- function(x, y) { sqrt(var(x * y,na.rm=T))/sqrt(sum(y,na.rm=T)) }
  
  ## Age
  c2.1 <- mean(data0812$age_2012); c2.2 <- sqrt(var(data0812$age_2012))/sqrt(nrow(data0812))
  c2.3 <- w.mean(data0812$age_2012, data0812$comp_w); c2.4 <- se.w.mean(data0812$age_2012, data0812$comp_w)
  
  ## Gender
  c3.1 <- mean(data0812$Male); c3.2 <- sqrt(var(data0812$Male))/sqrt(nrow(data0812))
  c3.3 <- w.mean(data0812$Male, data0812$comp_w); c3.4 <- se.w.mean(data0812$Male, data0812$comp_w)
  
  ## Black
  c4.1 <- mean(data0812$Black); c4.2 <- sqrt(var(data0812$Black))/sqrt(nrow(data0812))
  c4.3 <- w.mean(data0812$Black, data0812$comp_w); c4.4 <- se.w.mean(data0812$Black, data0812$comp_w)
  
  ## White
  c5.1 <- mean(data0812$White); c5.2 <- sqrt(var(data0812$White))/sqrt(nrow(data0812))
  c5.3 <- w.mean(data0812$White, data0812$comp_w); c5.4 <- se.w.mean(data0812$White, data0812$comp_w)
  
  ## any_drug_2
  c6.1 <- mean(data0812$any_drug_2); c6.2 <- sqrt(var(data0812$any_drug_2))/sqrt(nrow(data0812))
  c6.3 <- w.mean(data0812$any_drug_2, data0812$comp_w); c6.4 <- se.w.mean(data0812$any_drug_2, data0812$comp_w)
  
  ## any_violent_2
  c7.1 <- mean(data0812$any_violent_2); c7.2 <- sqrt(var(data0812$any_violent_2))/sqrt(nrow(data0812))
  c7.3 <- w.mean(data0812$any_violent_2, data0812$comp_w); c7.4 <- se.w.mean(data0812$any_violent_2, data0812$comp_w)
  
  ## any_rob_2
  c8.1 <- mean(data0812$any_rob_2); c8.2 <- sqrt(var(data0812$any_rob_2))/sqrt(nrow(data0812))
  c8.3 <- w.mean(data0812$any_rob_2, data0812$comp_w); c8.4 <- se.w.mean(data0812$any_rob_2, data0812$comp_w)
  
  ## any_dui_2
  c9.1 <- mean(data0812$any_dui_2); c9.2 <- sqrt(var(data0812$any_dui_2))/sqrt(nrow(data0812))
  c9.3 <- w.mean(data0812$any_dui_2, data0812$comp_w); c9.4 <- se.w.mean(data0812$any_dui_2, data0812$comp_w)
  
  ## fire_arms_2
  c10.1 <- mean(data0812$fire_arms_2); c10.2 <- sqrt(var(data0812$fire_arms_2))/sqrt(nrow(data0812))
  c10.3 <- w.mean(data0812$fire_arms_2, data0812$comp_w); c10.4 <- se.w.mean(data0812$fire_arms_2, data0812$comp_w)
  
  ## prior_2
  c11.1 <- mean(data0812$prior_offender_2); c11.2 <- sqrt(var(data0812$prior_offender_2))/sqrt(nrow(data0812))
  c11.3 <- w.mean(data0812$prior_offender_2, data0812$comp_w); c11.4 <- se.w.mean(data0812$prior_offender_2, data0812$comp_w)
  
  ## OGS
  c12.1 <- mean(data0812$totOGS, na.rm=T); c12.2 <- sqrt(var(data0812$totOGS, na.rm=T))/sqrt(nrow(data0812))
  c12.3 <- w.mean(data0812$totOGS, data0812$comp_w); c12.4 <- se.w.mean(data0812$totOGS, data0812$comp_w)
  
  # bail denied
  c13.1 <- mean(data0812$bail_denied, na.rm=T); c13.2 <- sqrt(var(data0812$bail_denied))/sqrt(nrow(data0812))
  c13.3 <- w.mean(data0812$bail_denied, data0812$comp_w); c13.4 <- se.w.mean(data0812$bail_denied, data0812$comp_w)
  
  
  ## voted 2012
  c1.1 <- mean(data0812$vote2012); c1.2 <- sqrt(var(data0812$vote2012))/sqrt(nrow(data0812))
  c1.3 <- w.mean(data0812$vote2012, data0812$comp_w); c1.4 <- se.w.mean(data0812$vote2012, data0812$comp_w)
  
  ## voted 2008
  c15.1 <- mean(data0812$vote2008); c15.2 <- sqrt(var(data0812$vote2008))/sqrt(nrow(data0812))
  c15.3 <- w.mean(data0812$vote2008, data0812$comp_w); c15.4 <- se.w.mean(data0812$vote2008, data0812$comp_w)
  
  ## not eligible 2008
  c16.1 <- mean(data0812$noteli08); c16.2 <- sqrt(var(data0812$noteli08))/sqrt(nrow(data0812))
  c16.3 <- w.mean(data0812$noteli08, data0812$comp_w); c16.4 <- se.w.mean(data0812$noteli08, data0812$comp_w)
  
  ## registered before
  c17.1 <- mean(data0812$regis_before); c17.2 <- sqrt(var(data0812$regis_before))/sqrt(nrow(data0812))
  c17.3 <- w.mean(data0812$regis_before, data0812$comp_w); c17.4 <- se.w.mean(data0812$regis_before, data0812$comp_w)
  
  ## quantile unk
  c18.1 <- mean(data0812$quant_unk); c18.2 <- sqrt(var(data0812$quant_unk))/sqrt(nrow(data0812))
  c18.3 <- w.mean(data0812$quant_unk, data0812$comp_w); c18.4 <- se.w.mean(data0812$quant_unk, data0812$comp_w)
  
  ## quantile 1
  c19.1 <- mean(data0812$quant_1); c19.2 <- sqrt(var(data0812$quant_1))/sqrt(nrow(data0812))
  c19.3 <- w.mean(data0812$quant_1, data0812$comp_w); c19.4 <- se.w.mean(data0812$quant_1, data0812$comp_w)
  
  ## quantile 2
  c20.1 <- mean(data0812$quant_2); c20.2 <- sqrt(var(data0812$quant_2))/sqrt(nrow(data0812))
  c20.3 <- w.mean(data0812$quant_2, data0812$comp_w); c20.4 <- se.w.mean(data0812$quant_2, data0812$comp_w)
  
  ## quantile 3
  c21.1 <- mean(data0812$quant_3); c21.2 <- sqrt(var(data0812$quant_3))/sqrt(nrow(data0812))
  c21.3 <- w.mean(data0812$quant_3, data0812$comp_w); c21.4 <- se.w.mean(data0812$quant_3, data0812$comp_w)
  
  tab <- rbind(
    cbind(c2.1, c2.2, c2.3, c2.4),
    cbind(c3.1, c3.2, c3.3, c3.4),
    cbind(c4.1, c4.2, c4.3, c4.4),
    cbind(c5.1, c5.2, c5.3, c5.4),
    cbind(c18.1, c18.2, c18.3, c18.4),
    cbind(c19.1, c19.2, c19.3, c19.4),
    cbind(c20.1, c20.2, c20.3, c20.4),
    cbind(c21.1, c21.2, c21.3, c21.4),
    cbind(c6.1, c6.2, c6.3, c6.4),
    cbind(c7.1, c7.2, c7.3, c7.4),
    cbind(c8.1, c8.2, c8.3, c8.4),
    cbind(c9.1, c9.2, c9.3, c9.4),
    cbind(c10.1, c10.2, c10.3, c10.4),
    cbind(c11.1, c11.2, c11.3, c11.4),
    cbind(c12.1, c12.2, c12.3, c12.4),
    cbind(c13.1, c13.2, c13.3, c13.4),
    cbind(c1.1, c1.2, c1.3, c1.4),
    cbind(c15.1, c15.2, c15.3, c15.4),
    cbind(c16.1, c16.2, c16.3, c16.4),
    cbind(c17.1, c17.2, c17.3, c17.4)
  )
  
  colnames(tab) <- c("Mean", "s.e.", "Weighted Mean", "s.e")
  rownames(tab) <- c("Age", "Male", "Black", "White",  
                     "Zip code income unknown", "Bottom tercile", "Middle tercile", "Top tercile" ,
                     "Drug-related", "Violence", "Robbery", "DUI",
                     "Firearms", "Prior Offense","Total OGS", "Bail Denied",
                     "Turnout 2012","Turnout 2008", "Eligibility 2008", "Registration Before Focal"
  )
  
  tab.o <- data.table(t(tab[, c(1, 3)]))
  #t(tab.o)
  round(t(tab.o),3)
  
  ## ---------------------------------------
  ## Bootstrap the Std. Errors
  ## ---------------------------------------
  run <- T
  if(run == T) {
    library(doParallel)
    nt <- detectCores()
    nc <- makeCluster(nt)
    registerDoParallel(nc)
    
    results <- foreach(i = 1:500) %dopar% {
      
      library('AER')
      library('ivpack')
      library('data.table')

      directory <- "../functions/"
      functions <- list.files(directory)  
      loadfunctions <- sapply(functions, FUN = function(x)source(paste0(directory, x)))
      
      load("../data0812.RData") 
      
      ## ---------------------------------------
      # Construct Instrument
      ## ---------------------------------------
      data0812 <- constructIV(data0812)
      
      ## ---------------------------------------
      #  Select Last Case Before Election
      ## ---------------------------------------
      data0812 <- lastCase(data0812)
      
      ## ---------------------------------------
      ## Construct additional covariate measures
      ## ---------------------------------------
      data0812$quant_1 <-ifelse(data0812$quants_inc == 1,1,0)
      data0812$quant_2 <-ifelse(data0812$quants_inc == 2,1,0)
      data0812$quant_3 <-ifelse(data0812$quants_inc == 3,1,0)
      data0812$quant_1[is.na(data0812$quant_1)] <- 0
      data0812$quant_2[is.na(data0812$quant_2)] <- 0
      data0812$quant_3[is.na(data0812$quant_3)] <- 0
      data0812$quant_unk <-ifelse(is.na(data0812$quants_inc) ,1,0)
      
      data0812$Black <- ifelse(data0812$race == "Black", 1, 0)
      data0812$White <- ifelse(data0812$race == "White", 1, 0)
      data0812$Male <- ifelse(data0812$Male == 1, 1, 0)
      
      if(group == "Black") {
        data0812 <- data0812[Black == 1, ]
      } 
      if(group == "White") {
        data0812 <- data0812[White == 1, ]
      }
      if(group == "Bottom Quantile") {
        data0812 <- data0812[quant_1 == 1, ]
      }
      if(group == "Mid Quantile") {
        data0812 <- data0812[quant_2 == 1, ]
      }
      if(group == "Upper Quantile") {
        data0812 <- data0812[quant_3 == 1, ]
      }
      
      set.seed(i)
      sample <- sample(1:nrow(data0812), nrow(data0812), replace = T)
      data0812 <- data0812[sample, ]
      
      ## ---------------------------------------
      ## Two Stage Least Squares Specifications
      ## ---------------------------------------
      
      ## ---------------------------------------
      ## 1. Main Specifications
      ## ---------------------------------------
      
      time.controls <- "as.factor(court_time1) + as.factor(court_time2) + as.factor(court_dow) + as.factor(court_shift)"
      case.controls <-   "as.factor(any_drug_2) + as.factor(fire_arms_2) +  as.factor(any_rob_2) + as.factor(any_dui_2) + as.factor(prior_offender_2)"
      case.controls_2 <- "as.factor(any_drug_2) +  as.factor(any_rob_2) + as.factor(any_dui_2)"
      demo.controls <- "age_2012 + I(age_2012^2) + Female + as.factor(race) + as.factor(vote2008) + as.factor(noteli08) + as.factor(regis_before)"
      demo.controls_2 <- "age_2012 + I(age_2012^2) + Female + as.factor(vote2008) + as.factor(noteli08) + as.factor(regis_before)"
      
      outc.1 <- "vote2012"
      endo.1 <- "pti"
      endo.2 <- "pti2"
      inst.1 <- "judgeiv"
      
      data0812$comp_w <- NA
      
      for(i in 1:3) {
        form.3.cc <- formula(paste(endo.1, "~", inst.1, "+", time.controls))
        out <- lm(form.3.cc, data = data0812[totOGS2 == i, ])  
        quants.iv <- quantile(data0812$judgeiv[data0812$totOGS2 == i], c(0.01, 0.99))
        frac_complier <- out$coefficients["judgeiv"] * quants.iv[2] - out$coefficients["judgeiv"] * quants.iv[1]
        mean_frac <- sum(data0812$totOGS2 == i)/nrow(data0812)
        data0812$comp_w[data0812$totOGS2 == i] <- frac_complier/mean_frac
      }
      
      w.mean <- function(x, y) { sum(x * y,na.rm=T)/sum(y,na.rm=T) }
      se.w.mean <- function(x, y) { sqrt(var(x * y,na.rm=T))/sqrt(sum(y,na.rm=T)) }
      
      ## Age
      c2.1 <- mean(data0812$age_2012); c2.2 <- sqrt(var(data0812$age_2012))/sqrt(nrow(data0812))
      c2.3 <- w.mean(data0812$age_2012, data0812$comp_w); c2.4 <- se.w.mean(data0812$age_2012, data0812$comp_w)
      
      ## Gender
      c3.1 <- mean(data0812$Male); c3.2 <- sqrt(var(data0812$Male))/sqrt(nrow(data0812))
      c3.3 <- w.mean(data0812$Male, data0812$comp_w); c3.4 <- se.w.mean(data0812$Male, data0812$comp_w)
      
      ## Black
      c4.1 <- mean(data0812$Black); c4.2 <- sqrt(var(data0812$Black))/sqrt(nrow(data0812))
      c4.3 <- w.mean(data0812$Black, data0812$comp_w); c4.4 <- se.w.mean(data0812$Black, data0812$comp_w)
      
      ## White
      c5.1 <- mean(data0812$White); c5.2 <- sqrt(var(data0812$White))/sqrt(nrow(data0812))
      c5.3 <- w.mean(data0812$White, data0812$comp_w); c5.4 <- se.w.mean(data0812$White, data0812$comp_w)
      
      ## any_drug_2
      c6.1 <- mean(data0812$any_drug_2); c6.2 <- sqrt(var(data0812$any_drug_2))/sqrt(nrow(data0812))
      c6.3 <- w.mean(data0812$any_drug_2, data0812$comp_w); c6.4 <- se.w.mean(data0812$any_drug_2, data0812$comp_w)
      
      ## any_violent_2
      c7.1 <- mean(data0812$any_violent_2); c7.2 <- sqrt(var(data0812$any_violent_2))/sqrt(nrow(data0812))
      c7.3 <- w.mean(data0812$any_violent_2, data0812$comp_w); c7.4 <- se.w.mean(data0812$any_violent_2, data0812$comp_w)
      
      ## any_rob_2
      c8.1 <- mean(data0812$any_rob_2); c8.2 <- sqrt(var(data0812$any_rob_2))/sqrt(nrow(data0812))
      c8.3 <- w.mean(data0812$any_rob_2, data0812$comp_w); c8.4 <- se.w.mean(data0812$any_rob_2, data0812$comp_w)
      
      ## any_dui_2
      c9.1 <- mean(data0812$any_dui_2); c9.2 <- sqrt(var(data0812$any_dui_2))/sqrt(nrow(data0812))
      c9.3 <- w.mean(data0812$any_dui_2, data0812$comp_w); c9.4 <- se.w.mean(data0812$any_dui_2, data0812$comp_w)
      
      ## fire_arms_2
      c10.1 <- mean(data0812$fire_arms_2); c10.2 <- sqrt(var(data0812$fire_arms_2))/sqrt(nrow(data0812))
      c10.3 <- w.mean(data0812$fire_arms_2, data0812$comp_w); c10.4 <- se.w.mean(data0812$fire_arms_2, data0812$comp_w)
      
      ## prior_2
      c11.1 <- mean(data0812$prior_offender_2); c11.2 <- sqrt(var(data0812$prior_offender_2))/sqrt(nrow(data0812))
      c11.3 <- w.mean(data0812$prior_offender_2, data0812$comp_w); c11.4 <- se.w.mean(data0812$prior_offender_2, data0812$comp_w)
      
      ## OGS
      c12.1 <- mean(data0812$totOGS, na.rm=T); c12.2 <- sqrt(var(data0812$totOGS, na.rm=T))/sqrt(nrow(data0812))
      c12.3 <- w.mean(data0812$totOGS, data0812$comp_w); c12.4 <- se.w.mean(data0812$totOGS, data0812$comp_w)
      
      # bail denied
      c13.1 <- mean(data0812$bail_denied, na.rm=T); c13.2 <- sqrt(var(data0812$bail_denied))/sqrt(nrow(data0812))
      c13.3 <- w.mean(data0812$bail_denied, data0812$comp_w); c13.4 <- se.w.mean(data0812$bail_denied, data0812$comp_w)
      
      
      ## voted 2012
      c1.1 <- mean(data0812$vote2012); c1.2 <- sqrt(var(data0812$vote2012))/sqrt(nrow(data0812))
      c1.3 <- w.mean(data0812$vote2012, data0812$comp_w); c1.4 <- se.w.mean(data0812$vote2012, data0812$comp_w)
      
      ## voted 2008
      c15.1 <- mean(data0812$vote2008); c15.2 <- sqrt(var(data0812$vote2008))/sqrt(nrow(data0812))
      c15.3 <- w.mean(data0812$vote2008, data0812$comp_w); c15.4 <- se.w.mean(data0812$vote2008, data0812$comp_w)
      
      ## not eligible 2008
      c16.1 <- mean(data0812$noteli08); c16.2 <- sqrt(var(data0812$noteli08))/sqrt(nrow(data0812))
      c16.3 <- w.mean(data0812$noteli08, data0812$comp_w); c16.4 <- se.w.mean(data0812$noteli08, data0812$comp_w)
      
      ## registered before
      c17.1 <- mean(data0812$regis_before); c17.2 <- sqrt(var(data0812$regis_before))/sqrt(nrow(data0812))
      c17.3 <- w.mean(data0812$regis_before, data0812$comp_w); c17.4 <- se.w.mean(data0812$regis_before, data0812$comp_w)
      
      ## quantile unk
      c18.1 <- mean(data0812$quant_unk); c18.2 <- sqrt(var(data0812$quant_unk))/sqrt(nrow(data0812))
      c18.3 <- w.mean(data0812$quant_unk, data0812$comp_w); c18.4 <- se.w.mean(data0812$quant_unk, data0812$comp_w)
      
      ## quantile 1
      c19.1 <- mean(data0812$quant_1); c19.2 <- sqrt(var(data0812$quant_1))/sqrt(nrow(data0812))
      c19.3 <- w.mean(data0812$quant_1, data0812$comp_w); c19.4 <- se.w.mean(data0812$quant_1, data0812$comp_w)
      
      ## quantile 2
      c20.1 <- mean(data0812$quant_2); c20.2 <- sqrt(var(data0812$quant_2))/sqrt(nrow(data0812))
      c20.3 <- w.mean(data0812$quant_2, data0812$comp_w); c20.4 <- se.w.mean(data0812$quant_2, data0812$comp_w)
      
      ## quantile 3
      c21.1 <- mean(data0812$quant_3); c21.2 <- sqrt(var(data0812$quant_3))/sqrt(nrow(data0812))
      c21.3 <- w.mean(data0812$quant_3, data0812$comp_w); c21.4 <- se.w.mean(data0812$quant_3, data0812$comp_w)
      
      tab <- rbind(
        cbind(c2.1, c2.2, c2.3, c2.4),
        cbind(c3.1, c3.2, c3.3, c3.4),
        cbind(c4.1, c4.2, c4.3, c4.4),
        cbind(c5.1, c5.2, c5.3, c5.4),
        cbind(c18.1, c18.2, c18.3, c18.4),
        cbind(c19.1, c19.2, c19.3, c19.4),
        cbind(c20.1, c20.2, c20.3, c20.4),
        cbind(c21.1, c21.2, c21.3, c21.4),
        cbind(c6.1, c6.2, c6.3, c6.4),
        cbind(c7.1, c7.2, c7.3, c7.4),
        cbind(c8.1, c8.2, c8.3, c8.4),
        cbind(c9.1, c9.2, c9.3, c9.4),
        cbind(c10.1, c10.2, c10.3, c10.4),
        cbind(c11.1, c11.2, c11.3, c11.4),
        cbind(c12.1, c12.2, c12.3, c12.4),
        cbind(c13.1, c13.2, c13.3, c13.4),
        cbind(c1.1, c1.2, c1.3, c1.4),
        cbind(c15.1, c15.2, c15.3, c15.4),
        cbind(c16.1, c16.2, c16.3, c16.4),
        cbind(c17.1, c17.2, c17.3, c17.4)
      )
      colnames(tab) <- c("Mean", "s.e.", "Weighted Mean", "s.e")
      rownames(tab) <- c("Age", "Male", "Black", "White",  
                         "Zip code income unknown", "Bottom tercile", "Middle tercile", "Top tercile" ,
                         "Drug-related", "Violence", "Robbery", "DUI",
                         "Firearms", "Prior Offense","Total OGS", "Bail Denied",
                         "Turnout 2012","Turnout 2008", "Eligibility 2008", "Registration Before Focal"
      )
      
      
      tab.b <- data.table(t(tab[, c(1, 3)]))
      tab.b$type <- c("mean", "w_mean")
      tab.b
    }
  }
  
  stopCluster(nc)
  
  
  results.c <- do.call('rbind', results)
  results.c1 <- results.c[type == "mean"]
  results.c2 <- results.c[type == "w_mean"]
  results.c1$type <- results.c2$type <- NULL
  
  cat("\nPrinting Table O2: Compliers Comparison...\n")
  cat(paste0("\nColumn: ", group, "...\n"))
  out <- data.table(round(t(tab.o),3))
  colnames(out) <- c("Sample_Means", "Compliers_Means")
  out$Sample_Means_SE <- t(round(apply(results.c1, 2, sd), 3))
  out$Compliers_Means_SE <- t(round(apply(results.c2, 2, sd), 3))
  out$Variable <- names(tab.o)
  print(out[, c("Variable", "Sample_Means", "Sample_Means_SE", "Compliers_Means", "Compliers_Means_SE")])
}

compliers(group = "All")
compliers(group = "Black")
compliers(group = "White")
compliers(group = "Bottom Quantile")
compliers(group = "Mid Quantile") 
compliers(group = "Upper Quantile")
